package controller

import (
	"encoding/base64"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"niu-video/app/model"
	"niu-video/app/service"
	"niu-video/pkg/response"
	"niu-video/pkg/validate"
	"strconv"
)

type VideoController struct {
}

// Token 获取上传视频的token
func (ctrl *VideoController) Token(c *gin.Context) {
	request := model.VideoUploadToken{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}

	resp := service.VideoService.GetUploadToken(request.Ticket)
	response.OkWithData(resp, c)
}

// GetImageByProxy 获取图片的base编码
func (ctrl *VideoController) GetImageByProxy(c *gin.Context) {
	imageURL := c.Query("url")
	resp, err := http.Get(imageURL)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	defer resp.Body.Close()

	imageData, err := io.ReadAll(resp.Body)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	base64Str := base64.StdEncoding.EncodeToString(imageData)

	response.OkWithData(base64Str, c)
}

// Publish 发布视频
func (ctrl *VideoController) Publish(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.PublishVideo{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}

	err := service.VideoService.Publish(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("发布成功！", c)
}

// List 获得视频列表
func (ctrl *VideoController) List(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.PageRequest{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.Recommend(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

// Category 分类的视频列表
func (ctrl *VideoController) Category(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.CategoryQuery{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.Category(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

func (ctrl *VideoController) Search(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.PageInfo{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.Search(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

func (ctrl *VideoController) UserWork(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.UserWorkQuery{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.UserWork(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

func (ctrl *VideoController) UserLike(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.UserWorkQuery{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.UserLike(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

func (ctrl *VideoController) UserCollect(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	request := model.UserWorkQuery{}
	if ok := validate.Validate(c, &request); !ok {
		return
	}
	list, err := service.VideoService.UserCollect(request, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(list, c)
}

func (ctrl *VideoController) Delete(c *gin.Context) {
	userId := c.GetInt64("current_user_id")
	videoId_ := c.Query("videoId")
	videoId, err := strconv.ParseInt(videoId_, 10, 64)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = service.VideoService.Delete(videoId, userId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}
